﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using Autodesk.Revit.DB;
using YArchitech.Controls;
using YArchitech.DataModel;
using YArchitech.LIB;
using YArchitech.LIB.YJKControls;
using YArchitech.Plumbing.Properties;

namespace YArchitech.Plumbing
{
	public partial class FormSupplyParasSetting : System.Windows.Forms.Form
	{
		public FormSupplyParasSetting(Document d, HYPipeCal pipe)
		{
			this.InitializeComponent();
			new YJKProcessTools().ShowInTaskbarChange(this);
			this.doc = d;
			//base.Icon = Resource.YArchitechIcon;
			this.sysRootPipe = pipe;
			this.InitParameterSetting();
		}

		private void radioButton1_CheckedChanged(object sender, EventArgs e)
		{
			if (this.radioButton1.Checked)
			{
				this.hyGroupA.Visible = true;
				this.hyGroup1.Visible = false;
				this.hyGroup2.Visible = false;
			}
		}

		private void radioButton2_CheckedChanged(object sender, EventArgs e)
		{
			if (this.radioButton2.Checked)
			{
				this.hyGroupA.Visible = false;
				this.hyGroup1.Visible = true;
				this.hyGroup2.Visible = false;
			}
		}

		private void radioButton3_CheckedChanged(object sender, EventArgs e)
		{
			if (this.radioButton3.Checked)
			{
				this.hyGroupA.Visible = false;
				this.hyGroup1.Visible = false;
				this.hyGroup2.Visible = true;
			}
		}

		private void btnOK_Click(object sender, EventArgs e)
		{
			if (this.radioButton1.Checked)
			{
				this.hySupplyFormula.formulaType = HYSupplyFormula.FormulaType.CALC_METHOD_ZZ;
				if (string.IsNullOrEmpty(this.tbTime.Text) || string.IsNullOrEmpty(this.tbNum.Text) || string.IsNullOrEmpty(this.tbWater.Text) || string.IsNullOrEmpty(this.tbEquiva.Text) || string.IsNullOrEmpty(this.hyTextBox3.Text))
				{
					YJKMessageBox.Information("参数不能空");
					return;
				}
				this.hySupplyFormula.dWaterNum = Convert.ToDouble(this.tbWater.Text);
				this.hySupplyFormula.nPersonNum = Convert.ToDouble(this.tbNum.Text);
				this.hySupplyFormula.dEquivalent = Convert.ToDouble(this.tbEquiva.Text);
				this.hySupplyFormula.dUserTime = Convert.ToDouble(this.tbTime.Text);
				this.hySupplyFormula.dHourCon = Convert.ToDouble(this.hyTextBox3.Text);
			}
			if (this.radioButton2.Checked)
			{
				this.hySupplyFormula.formulaType = HYSupplyFormula.FormulaType.CALC_METHOD_AMOUNT;
				if (string.IsNullOrEmpty(this.tbA.Text))
				{
					YJKMessageBox.Information("参数不能空");
					return;
				}
				this.hySupplyFormula.a = Convert.ToDouble(this.tbA.Text);
			}
			if (this.radioButton3.Checked)
			{
				this.hySupplyFormula.formulaType = HYSupplyFormula.FormulaType.CALC_METHOD_PER;
				this.hySupplyFormula.szBuildType = this.cobUse.Text;
			}
			this.hyCalParameter = new HYCalParameter();
			if (this.tbC.Text != "")
			{
				this.hyCalParameter.c = double.Parse(this.tbC.Text);
				this.hySupplyFormula.djz = double.Parse(this.tbJz.Text) / 100.0;
				this.djz = double.Parse(this.tbJz.Text) / 100.0;
				this.UserSettingWrite();
				base.DialogResult = DialogResult.OK;
				return;
			}
			YJKMessageBox.Information("参数不能为空");
		}

		private void btnCancel_Click(object sender, EventArgs e)
		{
			base.Close();
		}

		private void InitParameterSetting()
		{
			try
			{
				this.cobUse.Items.Clear();
				this.cobUse.Items.Add("工业企业生活间");
				this.cobUse.Items.Add("宿舍（Ⅲ、Ⅳ）");
				this.cobUse.Items.Add("公共浴室");
				this.cobUse.Items.Add("影剧院");
				this.cobUse.Items.Add("体育场馆");
				this.cobUse.Items.Add("职工食堂、营业餐馆");
				this.cobUse.Items.Add("实验室");
				this.pictureBox1.Image = YArchitech.Plumbing.Properties.Resources.Pic2;
				this.pictureBox2.Image = YArchitech.Plumbing.Properties.Resources.Pic3;
				this.hyGroup1.Location = this.hyGroupA.Location;
				this.hyGroup2.Location = this.hyGroupA.Location;
				this.dic = this.userSetting.GetSupplyCalSetting();
				if (this.userSetting.GetValueFromDic("nFormula", "0", this.dic) == "0")
				{
					this.radioButton1.Checked = true;
					this.hySupplyFormula.formulaType = HYSupplyFormula.FormulaType.CALC_METHOD_ZZ;
				}
				else if (this.userSetting.GetValueFromDic("nFormula", "0", this.dic) == "1")
				{
					this.radioButton2.Checked = true;
					this.hySupplyFormula.formulaType = HYSupplyFormula.FormulaType.CALC_METHOD_AMOUNT;
				}
				else
				{
					this.radioButton3.Checked = true;
					this.hySupplyFormula.formulaType = HYSupplyFormula.FormulaType.CALC_METHOD_PER;
				}
				this.tbWater.Text = this.userSetting.GetValueFromDic("tbWater", "300", this.dic);
				this.tbNum.Text = this.userSetting.GetValueFromDic("nPersonNum", "3.5", this.dic);
				this.tbEquiva.Text = this.userSetting.GetValueFromDic("tbEquiva", "40", this.dic);
				this.tbTime.Text = this.userSetting.GetValueFromDic("dUserTime", "24", this.dic);
				this.hyTextBox3.Text = this.userSetting.GetValueFromDic("hyTextBox3", "2.5", this.dic);
				this.tbA.Text = this.userSetting.GetValueFromDic("tbA", "1.5", this.dic);
				this.szBuildTypeOld = this.userSetting.GetValueFromDic("szBuildType", "工业企业生活间", this.dic);
				int num = this.cobUse.FindStringExact(this.szBuildTypeOld);
				if (num < 0)
				{
					num = 0;
				}
				this.cobUse.SelectedIndex = num;
				this.tbC.Text = this.userSetting.GetValueFromDic("c", "140", this.dic);
				this.tbJz.Text = this.userSetting.GetValueFromDic("dJz", "30", this.dic);
				this.hySupplyFormula.dWaterNum = Convert.ToDouble(this.tbWater.Text);
				this.hySupplyFormula.nPersonNum = Convert.ToDouble(this.tbNum.Text);
				this.hySupplyFormula.dEquivalent = Convert.ToDouble(this.tbEquiva.Text);
				this.hySupplyFormula.dUserTime = Convert.ToDouble(this.tbTime.Text);
				this.hySupplyFormula.dHourCon = Convert.ToDouble(this.hyTextBox3.Text);
				this.hySupplyFormula.a = Convert.ToDouble(this.tbA.Text);
				this.hySupplyFormula.szBuildType = this.cobUse.Text;
				this.hyCalParameter.c = double.Parse(this.tbC.Text);
				this.hySupplyFormula.djz = double.Parse(this.tbJz.Text) / 100.0;
				this.djz = double.Parse(this.tbJz.Text) / 100.0;
			}
			catch
			{
			}
		}

		private void UserSettingWrite()
		{
			try
			{
				Dictionary<string, string> dictionary = new Dictionary<string, string>();
				if (this.radioButton1.Checked)
				{
					dictionary.Add("nFormula", "0");
					this.hySupplyFormula.dWaterNum = Convert.ToDouble(this.tbWater.Text);
					this.hySupplyFormula.nPersonNum = Convert.ToDouble(this.tbNum.Text);
					this.hySupplyFormula.dEquivalent = Convert.ToDouble(this.tbEquiva.Text);
					this.hySupplyFormula.dUserTime = Convert.ToDouble(this.tbTime.Text);
					this.hySupplyFormula.dHourCon = Convert.ToDouble(this.hyTextBox3.Text);
					dictionary.Add("tbWater", this.tbWater.Text);
					dictionary.Add("nPersonNum", this.tbNum.Text);
					dictionary.Add("tbEquiva", this.tbEquiva.Text);
					dictionary.Add("dUserTime", this.tbTime.Text);
					dictionary.Add("hyTextBox3", this.hyTextBox3.Text);
				}
				else if (this.radioButton2.Checked)
				{
					dictionary.Add("nFormula", "1");
					dictionary.Add("tbA", this.tbA.Text);
				}
				else if (this.radioButton3.Checked)
				{
					dictionary.Add("nFormula", "2");
					dictionary.Add("szBuildType", this.cobUse.Text);
				}
				dictionary.Add("c", this.tbC.Text);
				dictionary.Add("dJz", this.tbJz.Text);
				this.userSetting.WriteSupplyCalSetting(dictionary);
			}
			catch (Exception)
			{
			}
		}

		private void button4_Click(object sender, EventArgs e)
		{
			if (this.floorUserList == null)
			{
				double num = 0.0;
				foreach (HYBathroom hybathroom in this.sysRootPipe.upBathroomArray)
				{
					num += hybathroom.dSupplyEquivalent * (double)hybathroom.nCount;
				}
				FormSupplyIntegratedBuild formSupplyIntegratedBuild = new FormSupplyIntegratedBuild(this.doc, num);
				if (formSupplyIntegratedBuild.ShowDialog() == DialogResult.OK)
				{
					this.tbA.Text = formSupplyIntegratedBuild.GetAValue();
					this.floorUserList = formSupplyIntegratedBuild.floorUserList;
					return;
				}
			}
			else
			{
				FormSupplyIntegratedBuild formSupplyIntegratedBuild2 = new FormSupplyIntegratedBuild(this.doc, this.floorUserList);
				if (formSupplyIntegratedBuild2.ShowDialog() == DialogResult.OK)
				{
					this.tbA.Text = formSupplyIntegratedBuild2.GetAValue();
					this.floorUserList = formSupplyIntegratedBuild2.floorUserList;
				}
			}
		}

		private void button5_Click(object sender, EventArgs e)
		{
			new FormSupplyBathroom(this.sysRootPipe).ShowDialog();
		}

		public void UpdataBathroomPer(string szOld)
		{
			try
			{
				string text = this.cobUse.Text;
				if (szOld != null && text != null && !(szOld == text))
				{
					Dictionary<string, int> supplyBathroomPer = HYCalSQLData.GetSupplyBathroomPer(text);
					foreach (HYBathroom hybathroom in this.sysRootPipe.upBathroomArray)
					{
						if (supplyBathroomPer.ContainsKey(hybathroom.szName))
						{
							hybathroom.dPer = (double)supplyBathroomPer[hybathroom.szName];
						}
					}
				}
			}
			catch
			{
			}
		}

		private void cobUse_SelectedIndexChanged(object sender, EventArgs e)
		{
			this.UpdataBathroomPer(this.szBuildTypeOld);
			this.szBuildTypeOld = this.cobUse.Text;
		}

		private void button1_Click(object sender, EventArgs e)
		{
			new FormSupplyShowPara("住宅最高日生活用水定额及小时变化系数", 1).ShowDialog();
		}

		private void button3_Click(object sender, EventArgs e)
		{
			new FormSupplyShowPara("根据建筑物用途而定的系数值", 2).ShowDialog();
		}

		private Document doc;

		public HYCalParameter hyCalParameter = new HYCalParameter();

		public HYSupplyFormula hySupplyFormula = new HYSupplyFormula();

		private Dictionary<string, string> dic;

		private HVACUserSetting userSetting = new HVACUserSetting();

		private List<SupplyFamilyCofi> list = HYCalSQLData.GetSupplyFamilyCoefficient();

		private Dictionary<int, string> dicName = HYCalSQLData.GetSupplyBuildingCoefficientName();

		private List<IntegratedBuildUser> floorUserList;

		private HYPipeCal sysRootPipe;

		public double djz = 0.3;

		public string szBuildTypeOld = "";
	}
}
